/*
 * $Id: DateUtils2.java,v 1.4 2010/06/08 07:51:03 ync Exp $
 * 
 * 文件名称：DateUtils2.java
 * 
 * 创建日期：2007-4-3
 * 
 * 版权所有：广州绿岛(www.gzld.net)
 */
/**
 * 提供所有与日期时间操作相关的公用函数
 * 
 * @author javen
 * 
 * @author allen
 * 
 * @version $Revision: 1.4 $ $Date: 2010/06/08 07:51:03 $
 */
package cn.org.timespace.easyframe.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

public class DateUtils2 {
	public final static String FM_PATTERN_CN_MD_HM = "MM月dd日 HH:mm";

    public final static String FM_PATTERN_CN_MD_NO = "MM月dd日";

    public final static String FM_PATTERN_CN_YMD_HM = "yyyy年MM月dd日 HH:mm";

    public final static String FM_PATTERN_CN_YMD_NO = "yyyy年MM月dd日";

    public final static String FM_PATTERN_CN_YM_NO = "yyyy年MM月";

    public final static String FM_PATTERN_EN_MD_HM = "MM-dd HH:mm";

    public final static String FM_PATTERN_EN_MD_NO = "MM-dd";

    public final static String FM_PATTERN_EN_YMD_HM = "yyyy/MM/dd HH:mm";

    public final static String FM_PATTERN_EN_YMD_NO = "yyyy/MM/dd";

    public final static String FM_PATTERN_EN_YM_NO = "yyyy/MM";
    /**
     * 返回系统日期
     * @return
     */
    public static java.sql.Date getNowDate() {
        Calendar cal = Calendar.getInstance();
        return new java.sql.Date(cal.getTimeInMillis());
    }
    /**
     * 返回系统时间
     * @return 形式为2007-04-17 11:15:33.372
     */
    public static java.sql.Timestamp getNowTimestamp() {
        Calendar cal = Calendar.getInstance();
        return new java.sql.Timestamp(cal.getTimeInMillis());
        
    }

    public static long getNowDateTimeLong() {
        Calendar cal = Calendar.getInstance();
        return cal.getTimeInMillis();
    }
    /**
     * 返回系统时间
     * @return 形式为：2007-04-17 11:15
     */
    public static String getNowDateTimeString() {
        Calendar cal = Calendar.getInstance();
        long millis = cal.getTimeInMillis();
        return formatDateLong(millis);
    }

    public static Date getDate(long millis) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(millis);
        return cal.getTime();
    }

    public static java.sql.Date getDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return new java.sql.Date(cal.getTimeInMillis());
    }

    public static java.sql.Date getDate(String dateString) {
        return java.sql.Date.valueOf(dateString);
    }

    public static String formatDateShort(long millis) {
        String pattern = "MM-dd HH:mm";
        String date = DateFormatUtils.format(millis, pattern);
        return date;
    }

    public static String formatDateLong(long millis) {
        String pattern = "yyyy-MM-dd HH:mm";
        String date = DateFormatUtils.format(millis, pattern, Locale.CHINA);
        return date;
    }

    public static String formatDateChinese(long millis) {
        String pattern = "yyyy-MM-dd HH:mm";
        String date = DateFormatUtils.format(millis, pattern, Locale.CHINA);
        return date;
    }

    public static String fotmatDate1(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy年MM月dd日 HH时mm分ss秒");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate2(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate3(Date myDate) {
        String strDate = null;
        if (myDate == null)
            strDate = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate4(Date myDate) {
        String strDate = null;
        if (myDate == null)
            strDate = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate5(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate6(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate7(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    /**
     * 将java.sql.Date类对象转换为java.sql.Timestamp类对象， 时间部分为"00:00:00.000"
     * 
     * @param date java.sql.Date 要转换的Date型对象
     * @return java.sql.Timestamp 转换后的Timestamp型对象
     */
    public static Timestamp convertDateToTimestampMin(java.sql.Date date) {
        return Timestamp.valueOf(date.toString() + " 00:00:00.000");
        
    }

    /**
     * 将java.sql.Date类对象转换为java.sql.Timestamp类对象， 时间部分为"23:59:59.999"
     * 
     * @param date java.sql.Date 要转换的Date型对象
     * @return java.sql.Timestamp 转换后的Timestamp型对象
     */
    public static Timestamp convertDateToTimestampMax(java.sql.Date date) {
        return Timestamp.valueOf(date.toString() + " 23:59:59.999");
    }

    /**
     * 功能：用于获取指定日期该月的所有日期
     * 
     * @param date java.sql.Date 要获取的月日期列表的指定日期
     * @return Date[] java.sql.Date 返回的日期列表
     */
    public static java.sql.Date[] getMonthDays(java.sql.Date date) {

        Calendar cale = Calendar.getInstance();
        cale.setTime(date);

        int today = cale.get(Calendar.DAY_OF_MONTH);
        int days = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
        long millis = cale.getTimeInMillis();

        java.sql.Date dates[] = new java.sql.Date[days];
        for (int i = 1; i <= days; i++) {
            long sub = (today - i) * 24 * 60 * 60 * 1000L;
            dates[i - 1] = new java.sql.Date(millis - sub);
        }

        cale = null;
        return dates;
    }

    /**
     * 功能：用于获取指定日期该周的所有日期
     * 
     * @param date java.sql.Date 要获取的周日期列表的指定日期
     * @return Date[] java.sql.Date 返回的日期列表
     */
    public static java.sql.Date[] getWeekDays(java.sql.Date date) {
    	//System.out.println(date.toString());
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.setFirstDayOfWeek(Calendar.SUNDAY);
        int days = 8;
        int today = cale.get(Calendar.DAY_OF_WEEK);
        long millis = cale.getTimeInMillis();

        java.sql.Date dates[] = new java.sql.Date[days];
        for (int i = 1; i <= days; i++) {
            long sub = (today - i) * 24 * 60 * 60 * 1000L;
            dates[i-1] = new java.sql.Date(millis - sub);
        }

        cale = null;
        return dates;
    }
    /**
     * 根据开始时间和结束时间返回相应的时间段字符串，如果开始时间和结束时间在同一天，
     * 则返回的格式为“X点X分-X点X分”，如果不在同一天，返回的格式为“X月X日-X月X日”
     * 
     * @param startTime 开始时间
     * 
     * @param endTime 结束时间
     * 
     * @return 返回的时间段字符串
     */
    public static String getTimeSlice(Timestamp startTime, Timestamp endTime) {

        String rtn = "";

        Calendar caleStart = Calendar.getInstance();
        Calendar caleEnd = Calendar.getInstance();
        caleStart.setTimeInMillis(startTime.getTime());
        caleEnd.setTimeInMillis(endTime.getTime());

        String dayStart = caleStart.get(Calendar.YEAR) + "年"
                + (caleStart.get(Calendar.MONTH) + 1) + "月"
                + caleStart.get(Calendar.DAY_OF_MONTH) + "日";
        String dayEnd = caleEnd.get(Calendar.YEAR) + "年"
                + (caleEnd.get(Calendar.MONTH) + 1) + "月"
                + caleEnd.get(Calendar.DAY_OF_MONTH) + "日";

        if (dayStart.equals(dayEnd)) {
            //同一天
            rtn = caleStart.get(Calendar.HOUR_OF_DAY) + "点"
                    + caleStart.get(Calendar.MINUTE) + "分-"
                    + caleEnd.get(Calendar.HOUR_OF_DAY) + "点"
                    + caleEnd.get(Calendar.MINUTE) + "分";
        } else {
            //不在同一天
            rtn = (caleStart.get(Calendar.MONTH) + 1) + "月"
                    + caleStart.get(Calendar.DAY_OF_MONTH) + "日" + "-"
                    + (caleEnd.get(Calendar.MONTH) + 1) + "月"
                    + caleEnd.get(Calendar.DAY_OF_MONTH) + "日";
        }

        caleStart = null;
        caleEnd = null;
        return rtn;
    }

    /**
     * 根据日期获得日期星期几格式的字符串，如“2004-07-28 星期三”
     * 
     * @param date 指定的日期
     * 
     * @return 返回的日期星期几格式的字符串
     */
    public static String getDayWeek(java.sql.Date date) {

        String days[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.setFirstDayOfWeek(Calendar.SUNDAY);

        return date.toString() + " " + days[cale.get(Calendar.DAY_OF_WEEK) - 1];
    }
    
    
    public static String getDayWeek2(java.sql.Date date) {

        String days[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.setFirstDayOfWeek(Calendar.SUNDAY);

        return  days[cale.get(Calendar.DAY_OF_WEEK) - 1];
    }
    
    

    /**
     * 获得指定日期所在月最小时间，时间部分为“00:00:00.000” 例如：param:2004-08-20 return:2004-08-01
     * 00.00.00.000
     * 
     * @param date 指定的日期
     * @return 指定日期所在月的最小时间
     */
    public static Timestamp getMinDayInMonth(java.sql.Date date) {

        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.set(Calendar.DAY_OF_MONTH, cale
                .getActualMinimum(Calendar.DAY_OF_MONTH));
        java.sql.Date newDate = new java.sql.Date(cale.getTimeInMillis());

        cale = null;
        return Timestamp.valueOf(newDate.toString() + " 00:00:00.000");

    }

    /**
     * 获得指定日期所在月的最大时间，时间部分为“23.59.59.999” 例如：param:2004-08-20,return:2004-08-31
     * 23.59.59.999
     * 
     * @param date
     * @return
     */
    public static Timestamp getMaxDayInMonth(java.sql.Date date) {

        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.set(Calendar.DAY_OF_MONTH, cale
                .getActualMaximum(Calendar.DAY_OF_MONTH));
        java.sql.Date newDate = new java.sql.Date(cale.getTimeInMillis());

        cale = null;
        return Timestamp.valueOf(newDate.toString() + " 23:59:59.999");
    }

    public static String strNowtime() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "yyyy-MM-dd-hh-mm-ss");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    
    public static String strNowtime1() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "y-MM-dd-hh-mm");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    
    public static String strNowtime2() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    
    public static String strNowtimeS() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "yyyy-MM-dd-hh-mm-ss-S");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    
    /*
     * 获取当前的小时数
     */
    public static int getHour(){
    	
    	Calendar c=Calendar.getInstance();
    	Date date=c.getTime();
    	return date.getHours();
    	
    }
    
   /*
    * 获取当前分钟
    */ 
    public static int getMinute(){
    	Calendar c=Calendar.getInstance();
    	Date date=c.getTime();
    	return date.getMinutes();
    	
    }
    
    

    /**
     * 得到当前年-月
     * @return
     */

    public static String GetPath() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "yyyy-MM");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    /**
     * 得到当前年-月
     * @return
     */

    public static String getYearMonth() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "yyMM");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }

    /**
     * 得到当前年
     * @return String
     */

    public static String getThisYear() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "yyyy");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    /**
     * 得到当前年-yy
     * @return String
     */

    public static String getThisYear1() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "yy");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    /**
     * 得到當前yymmdd
     * @return String
     */

    public static String getThisTime() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "yyMMdd");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    /**
     * 得到当前日
     * @return String
     */

    public static String getThisDay() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "dd");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }
    /**
     * 得到当前月
     * @return String
     */

    public static String getThisMonth() {
        String dateString = "";
        try {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                    "MM");
            java.util.Date currentTime_1 = new java.util.Date();
            dateString = formatter.format(currentTime_1);
        } catch (Exception e) {
        }
        return dateString;
    }

   
    // 得到当前季度
    
    public static String getThisQuarter() {
        String quarter = "";
        String month = getThisMonth();
        if (month.equals("01") || month.equals("02") || month.equals("03")) {
            quarter = "第一季度";
        } else if (month.equals("04") || month.equals("05")
                || month.equals("06")) {
            quarter = "第二季度";
        } else if (month.equals("07") || month.equals("08")
                || month.equals("09")) {
            quarter = "第三季度";
        } else {
            quarter = "第四季度";
        }
        return quarter;
    }
    
    //判断是不是润年
    public static boolean SmoothYear(){
    	boolean smoothYear=true;
    	int year=Integer.parseInt(getThisYear());
    	if(year%4==0&&year%100==0){
    		if(year%400==0){
    			smoothYear=false;
    		}
    		
    	}else{
    		smoothYear=false;
    	}
    	return smoothYear;

    }
    //判断每个月最多有多少天
    public static String getMaxDays(){
    	
    	String maxDays="";
    	int month=Integer.parseInt(getThisMonth());
    	switch(month){
    	  case 1:maxDays="31";break;
    	  case 2:if(SmoothYear())maxDays="29";else maxDays="28";break;
    	  case 3:maxDays="31";break;
    	  case 4:maxDays="30";break;
    	  case 5:maxDays="31";break;
    	  case 6:maxDays="30";break;
    	  case 7:maxDays="31";break;
    	  case 8:maxDays="31";break;
    	  case 9:maxDays="30";break;
    	  case 10:maxDays="31";break;
    	  case 11:maxDays="30";break;
    	  case 12:maxDays="31";break;	  
    	
    	
    	
    	}
    	return maxDays;
    	
    }
    //判断每个月最多有多少天
    public static String getMaxDays(String Month){
    	
    	String maxDays="";
    	int month=Integer.parseInt(Month);
    	switch(month){
    	  case 1:maxDays="31";break;
    	  case 2:if(SmoothYear())maxDays="29";else maxDays="28";break;
    	  case 3:maxDays="31";break;
    	  case 4:maxDays="30";break;
    	  case 5:maxDays="31";break;
    	  case 6:maxDays="30";break;
    	  case 7:maxDays="31";break;
    	  case 8:maxDays="31";break;
    	  case 9:maxDays="30";break;
    	  case 10:maxDays="31";break;
    	  case 11:maxDays="30";break;
    	  case 12:maxDays="31";break;	  
    	
    	
    	
    	}
    	return maxDays;
    	
    }   
    //判断每个月的第一天是星期几
    
    public static String getWeekDay(){
    	
    	String weekDay="";
    	String thisyear=getThisYear();
    	String thismonth=getThisMonth();
    	String date=thisyear+"-"+thismonth+"-"+"01";
    	Date da=getDate(date);
    	weekDay=Integer.toString(da.getDay());
    	return weekDay;
    	
    }
    
    /**
     * 根据stringdate--->Timestamp
     * @param dateString
     * @param seperator
     * @return
     */
    public static Timestamp stringParseTimestamp(String dateString,
			char seperator) {
		if ("".equals(dateString) || null == dateString)
			return null;
		DateFormat format = new SimpleDateFormat("yyyy" + seperator + "MM"
				+ seperator + "dd" + " hh:mm");
		Date now = null;
		try {
			now = format.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		return new Timestamp(cal.getTimeInMillis());
	}
    
    
//  日期与年龄的转化
	/**
	 * 根据生日算出现在的年龄
	 */
    public static String getAgeNum(String birthday) {
        String stempyear="";
        String stempmonth="";
        int itemp=0;
        int imonth;
        if (birthday!=null) {
            
            String[] birthdays = StringUtils.split(birthday,'-');
            stempyear = birthdays[0];
            stempmonth = birthdays[1];
            imonth=Calendar.getInstance().get(Calendar.MONTH)+1;
            if (Integer.parseInt(stempmonth)<imonth) {
                 itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear);
                
            } else {
                 itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear)-1;
            }
           
            if (itemp<=0) {
                itemp=1;
            }
            
        }
        
        return itemp+"";
    }
    
    /**
	 * 根据生日算出现在的年龄
	 */
    public static String getAgeNumYear(String birthday) {
    	
        String stempyear="";
        String stempmonth="";
        int itemp=0;
        int imonth;
        birthday=StringUtils2.convertNullToString(birthday);
//        if("".equals(birthday) || birthday.indexOf("/")>=0){
//        	itemp=0;
//        }else{
        	
            if (!birthday.equals("")&&birthday.length()>=4) {
//                int size=birthday.length();
//          	  String[] birthdays = StringUtils.split(birthday,'-');
//              stempyear = birthdays[0];
//              stempmonth = birthdays[1];
                
//            	if(size>4){
//                  stempyear = birthday.substring(0, 4);
//                  stempmonth = birthday.substring(4, 6);
//            	}else{
//            	  stempyear = birthday.substring(0, 4);
//                  stempmonth = "1";
//            	}
//                imonth=Calendar.getInstance().get(Calendar.MONTH)+1;
//                if (Integer.parseInt(stempmonth)<=imonth) {
//                     itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear);
//                    
//                } else {
//                     itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear)-1;
//                }
            	if(birthday.lastIndexOf("/")>=0)
            	{
            		stempyear=(birthday.substring(birthday.lastIndexOf("/")+1)).length()==4?birthday.substring(birthday.lastIndexOf("/")+1):"-";
            		if(stempyear.indexOf("-")>=0){
            			itemp = 0;
            		}else
            		{
            			try{
//                    		itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear)-1;
            				itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear);
                    	}catch (Exception e) {
                			e.printStackTrace();
                			itemp=0;
                		}
            		}
            	}
            	else
            	{
            		stempyear = birthday.substring(0, 4);
            		if(stempyear.indexOf("/")>=0 || stempyear.indexOf("-")>=0){
            			itemp = 0;
            		}else{
            			try{
//            				itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear)-1;
            				itemp=Calendar.getInstance().get(Calendar.YEAR)-Integer.parseInt(stempyear);
            			}catch (Exception e) {
            				e.printStackTrace();
            				itemp=0;
            			}
            		}
            	}
//                if (itemp<=0) {
//                    itemp=1;
//                }
                
            }
//        }
        
        return itemp+"";
    }
    
	/**
	 * 根据参数获取系统时间的前N天或者后N天
	 * @param day    当day为正时:代表获取系统时间的后day天  当day为负时:代表获取系统时间的前day天
	 * @return
	 */
	public static String getTimeSegment(int day) {
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.YEAR, Integer.parseInt(DateUtils2.getThisYear()));
		cal.set(Calendar.MONTH, Integer.parseInt(DateUtils2.getThisMonth())-1);
		cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(DateUtils2.getThisDay()));
		Date date=cal.getTime();
		cal.add(Calendar.DATE, day);
		date=cal.getTime();
		return df.format(date);
	}
    
    
    /**
     * 转换负数  Double
     * @param birthday
     * @return
     */
    public static String getNegative(String num) {
    	return Double.toString(MathUtils.sub(Double.parseDouble(num),Double.parseDouble(num)*2));
    }
    /**
     * 转换负数  int
     * @param birthday
     * @return
     */
    public static String getNegativeInt(String num) {
    	return String.valueOf(Integer.parseInt(num)-Integer.parseInt(num)*2);
    }
    
    public static java.sql.Date getDateSql(String dateString) throws ParseException {
    	
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
    	Date date = sdf.parse( dateString ); 
    	
    	return  new java.sql.Date(date.getTime()); 
    }
    /**
     * 得到YYMMDDHH24MiSS格式的date
     * */
    public static java.sql.Date getDateSql1(String dateString)throws ParseException {
    	
    	SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss"); 
    	Date date = sdf.parse( dateString );
    	return  new java.sql.Date(date.getTime()); 
    }

    /**   
     * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式   
     * @param date2 被比较的时间  为空(null)则为当前时间   
     * @param stype 返回值类型   0为多少天，1为多少个月，2为多少年   
     * @return   
     */  
    public static int compareDate(String date1,String date2,int stype){   
        int n = 0;   
           
        String[] u = {"天","月","年"};   
        String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";   
           
        date2 = date2==null?DateUtils2.getCurrentDate():date2;   
           
        DateFormat df = new SimpleDateFormat(formatStyle);   
        Calendar c1 = Calendar.getInstance();   
        Calendar c2 = Calendar.getInstance();   
        try {   
            c1.setTime(df.parse(date1));   
            c2.setTime(df.parse(date2));   
        } catch (Exception e3) {   
            System.out.println("wrong occured");   
        }   
        //List list = new ArrayList();   
        while (!c1.after(c2)) {                     // 循环对比，直到相等，n 就是所要的结果   
            //list.add(df.format(c1.getTime()));    // 这里可以把间隔的日期存到数组中 打印出来   
            n++;   
            if(stype==1){   
                c1.add(Calendar.MONTH, 1);          // 比较月份，月份+1   
            }   
            else{   
                c1.add(Calendar.DATE, 1);           // 比较天数，日期+1   
            }   
        }   
           
        //n = n-1;   
           
        if(stype==2){   
            n = (int)n/365;   
        }      
           
       // System.out.println(date1+" -- "+date2+" 相差多少"+u[stype]+":"+n);         
        return n;   
    }   
       
    /**   
     * 得到当前日期   
     * @return   
     */  
    public static String getCurrentDate() {   
        Calendar c = Calendar.getInstance();   
        Date date = c.getTime();   
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");   
        return simple.format(date);   

    }
    
    /**   
     * 得到当前时间   
     * @return   
     */  
    public static String getCurrentTime() {   
        Calendar c = Calendar.getInstance();   
        Date date = c.getTime();   
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        return simple.format(date);   

    } 
    
    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
     * 
     * @param dateDate
     * @return
     */
  public static String dateToStrLong(java.util.Date dateDate) {
     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     String dateString = formatter.format(dateDate);
     return dateString;
  }
  
  public static String converttolong(String date) 
  {
	  int leng=date.length();
	  if(leng<5)
	  {
		  date="0"+date+":00";
	  }
	  else
	  {
		  date=date+":00";
	  }
	  return date;
  }
  public static Date strToDateLong(String strDate) {
  	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  	   ParsePosition pos = new ParsePosition(0);
  	   Date strtodate = formatter.parse(strDate, pos);
  	   return strtodate;
  }
  
  public static Date strToDateLong2(String strDate) {
 	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 	   ParsePosition pos = new ParsePosition(0);
 	   Date strtodate = formatter.parse(strDate, pos);
 	   return strtodate;
 }
  /**
   * 获取现在时间
   * 
   * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
 * @throws ParseException 
   */
public static java.sql.Date getNowTime() throws ParseException {
   Date currentTime = new Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String dateString = formatter.format(currentTime);
//   ParsePosition pos = new ParsePosition(2);
   java.util.Date currentTime_2 = formatter.parse(dateString);
  
   return new java.sql.Date(currentTime_2.getTime());
   
}


public static java.sql.Date getDate() throws ParseException {
	   Date currentTime = new Date();
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	   String dateString = formatter.format(currentTime);
	//   ParsePosition pos = new ParsePosition(2);
	   java.util.Date currentTime_2 = formatter.parse(dateString);
	  
	   return new java.sql.Date(currentTime_2.getTime());
	   
	}

/**
 * 生成随机字符串. <br>
 * 随机字符串的内容包含[0-9]的字符. <br>
 * 
 * @param randomLength
 *            随机字符串的长度
 * @return 随机字符串.
 */
public static String randomChars(int randomLength) {
    char[] randoms = { '0','1', '2', '3',
            '4', '5', '6', '7', '8', '9' };
    Random random = new Random();
    StringBuffer ret = new StringBuffer();
    for (int i = 0; i < randomLength; i++) {
        ret.append(randoms[random.nextInt(randoms.length)]);
    }
    random = null;
    return ret.toString();
}

public static String strNowtimeSi() {
    String dateString = "";
    try {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
                "yyMMddHHmmssS");
        java.util.Date currentTime_1 = new java.util.Date();
        dateString = formatter.format(currentTime_1);
    } catch (Exception e) {
    }
    return dateString;
}
/**
 * 獲得當前日期的下一周
 */
private static int getMondayPlus() 
{
	Calendar cd = Calendar.getInstance();
    int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;
	if (dayOfWeek == 1)
	{
		return 0;
	}
	else 
	{
		return 1 - dayOfWeek;  
	}  
}  

public static String getNextMonday()
{
	int mondayPlus = getMondayPlus();  
	GregorianCalendar currentDate = new GregorianCalendar();  
	currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
	Date monday = currentDate.getTime();  
//	DateFormat df = DateFormat.getDateInstance();  
//	String preMonday = df.format(monday);  
//	return preMonday;  
	return fotmatDate4(monday);  
}

public static String getNextTuesday()
{
	int mondayPlus = getMondayPlus();  
	GregorianCalendar currentDate = new GregorianCalendar();  
	currentDate.add(GregorianCalendar.DATE, mondayPlus + 8);
	Date monday = currentDate.getTime();  
//	DateFormat df = DateFormat.getDateInstance();  
//	String preMonday = df.format(monday);  
//	return preMonday;  
	return fotmatDate4(monday); 
}

public static String getNextWednesday()
{
	int mondayPlus = getMondayPlus();  
	GregorianCalendar currentDate = new GregorianCalendar();  
	currentDate.add(GregorianCalendar.DATE, mondayPlus + 9);
	Date monday = currentDate.getTime();  
//	DateFormat df = DateFormat.getDateInstance();  
//	String preMonday = df.format(monday);  
//	return preMonday;  
	return fotmatDate4(monday); 
}  

public static String getNextThursday()
{
	int mondayPlus = getMondayPlus();  
	GregorianCalendar currentDate = new GregorianCalendar();  
	currentDate.add(GregorianCalendar.DATE, mondayPlus + 10);
	Date monday = currentDate.getTime();  
//	DateFormat df = DateFormat.getDateInstance();  
//	String preMonday = df.format(monday);  
//	return preMonday; 
	return fotmatDate4(monday); 
}  

public static String getNextFriday()
{
	int mondayPlus = getMondayPlus();  
	GregorianCalendar currentDate = new GregorianCalendar();  
	currentDate.add(GregorianCalendar.DATE, mondayPlus + 11);
	Date monday = currentDate.getTime();  
//	DateFormat df = DateFormat.getDateInstance();  
//	String preMonday = df.format(monday);  
//	return preMonday;  
	return fotmatDate4(monday); 
} 

public static String getNextSaturday()
{
	int mondayPlus = getMondayPlus();  
	GregorianCalendar currentDate = new GregorianCalendar();  
	currentDate.add(GregorianCalendar.DATE, mondayPlus + 12);
	Date monday = currentDate.getTime();  
//	DateFormat df = DateFormat.getDateInstance();  
//	String preMonday = df.format(monday);  
//	return preMonday;  
	return fotmatDate4(monday); 
} 
	      
public static String getNextSunday() 
{  
    int mondayPlus = getMondayPlus();  
    GregorianCalendar currentDate = new GregorianCalendar();  
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);  
    Date monday = currentDate.getTime();  
//  DateFormat df = DateFormat.getDateInstance();  
//  String preMonday = df.format(monday);  
//  return preMonday; 
    return fotmatDate4(monday); 
}  

/**
 * 根据上次发送的时间和这次传入的时间相比，是不是等于或大于可以发送短信的间隔时间(分钟)
 * @param brushcarddate
 * @param time
 * @param spaceTimes
 * @return
 */
public static boolean getTimeSpace(String brushcarddate,String time,String spaceTimes){
	boolean flag = false;
	Date d1 =DateUtils2.strToDateLong(brushcarddate);//上次发送时间
	Date d2 =DateUtils2.strToDateLong(time);//这次传入的时间
	long diff = d2.getTime() - d1.getTime();//时间差  1分钟 = 60秒 = 60*1000毫秒
	diff = Math.abs(diff);
    long spaceTime = Long.parseLong(spaceTimes)*60*1000;
    long count = diff-spaceTime;
    if(count >0){
    	flag = true;
    }else{
    	flag = false;
    }
	return flag;
}

public static void main(String args[]) throws ParseException{
//	System.out.print(DateUtils2.getYearMonth());
//	System.out.println(getAgeNumYear("1990-02-09"));
//	System.out.println(getAgeNumYear("19900209"));
//	System.out.println(getAgeNumYear("02/09/1990"));
	
	//int aa=DateUtils2.getHour();
	String  aa = "cz010";
	try{
		//int ii = Integer.valueOf(aa.substring(0, 2));
		String contentStr="CZ0000938096+1000";
		String arr[] = contentStr.split("\\+");
		System.out.println(arr[0]);
	}catch(NumberFormatException e){
		
		e.printStackTrace();
	}
	
}
}










